package com.lsc.job;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.lsc.entity.BugContent;
import com.lsc.service.BugCatchService;
import com.lsc.service.FileUtil;
import com.lsc.service.MailSendService;

@Configuration
@EnableScheduling
public class BugCatchJob {

	@Autowired
	BugCatchService ss;

	@Autowired
	MailSendService mailSendService;

	@Value("${bug.file}")
	String checkFilePath;

	@Scheduled(cron = "0 0 0 1/1 * ?")  //0 0 0 1/1 * ?
	private void catchBug() {
		System.out.println("=====job start====");
		try {
			List<BugContent> buglist = new ArrayList();
			File checkFile = new File(checkFilePath + "/checkfile.txt");
			String checkTime = FileUtil.readTxt(checkFile);

			ss.bugCatch(1, checkTime, buglist);
			
			StringBuffer text = new StringBuffer();
			if (buglist.size() > 0) {
				text.append("检查日期：" + checkTime + "\r\n");
				text.append("捕获相关漏洞：" + buglist.size() + "条" + "\r\n");
				text.append("============漏洞详情如下============" + "\r\n");
				for (int i = 0; i < buglist.size(); i++) {
					text.append("(" + buglist.get(i).getBugLev() + ")  " + buglist.get(i).getBugTitle() + "  ["
							+ buglist.get(i).getBugNum() + "]======" + buglist.get(i).getBugTime() + "\r\n");
					text.append(buglist.get(i).getBugjs() + "\r\n");
					text.append("===================================" + "\r\n");
				}
				mailSendService.sendMail("[" + checkTime + "]漏洞情况汇总", text.toString());
			}
			System.out.println("检查日期：" + checkTime);
			System.out.println("捕获相关漏洞：" + buglist.size() + "条");
			System.out.println("=====job end====");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
